package com.systop.ltbook.sms.smsproxy;

import java.rmi.RemoteException;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.systop.ltbook.sms.SmsConstants;
import com.systop.ltbook.sms.model.SmsSend;
import com.systop.ltbook.sms.smsproxy.webservice.SMSPlatformService;
import com.systop.ltbook.sms.smsproxy.webservice.SMSPlatformServiceProxy;

@Service("smsProxy")
public class SmsProxyImpl implements SmsProxy {

	private static Logger logger = Logger.getLogger(SmsProxyImpl.class);
	
	/**
	 * 发送单个短信方法
   * 
   * @param smsSend 要发送的短信实体实例
   * @return 发送到短信系统中的短信id
   * @throws Exception 短信系统的WebService会抛出RemoteException.<br>
   *         这个异常无法指望本系统进行处理,也不应该由本系统进行处理<br>
   *         所以对于本方法的:throws Exception,只管在方法中继续throws Exception
	 */
	@Override
  public Integer sendMessage(SmsSend smsSend) throws Exception {
		SMSPlatformService service = new SMSPlatformServiceProxy();
		String[] mobiles = new String[] { smsSend.getMobileNum() };
		String reInfoId = "reInfoId";
		try {
			int state = service.sendMSG(mobiles, smsSend.getContent(), 
					SmsConstants.BILL_CODE, SmsConstants.SERIAL_NU, 
					SmsConstants.SEND_TIME, SmsConstants.SERIAL_PW, reInfoId);
			logger.debug("发送后返回状态码:"+state);
			if (state >= 0) {
				return state;
			} else {
				StringBuffer buf = new StringBuffer();
	      buf.append("ID为:").append(smsSend.getId()).append(",接收号码为").append(
	          smsSend.getMobileNum()).append("的短信发送失败!").append("错误原因为:接收到id值")
	          .append(state);
	      logger.error(buf.toString());
	      throw new Exception(buf.toString());
			} 
		} catch (RemoteException ex) {
      logger.error(ex.getMessage());
      throw new Exception(ex);
    }
  }

	@Override
  public void querySmsSendState(SmsSend smsSend) throws Exception {
	  // TODO Auto-generated method stub
	  
  }
	
	public static void main(String [] args) {
		SmsProxyImpl ss = new SmsProxyImpl();
		SmsSend smsSend = new SmsSend();
		try {
	    ss.sendMessage(smsSend);
    } catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
    }
	}
}
